package it3807.semOppg1;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;

public class DocSearch {
	
	public final static String[] ALL_FIELDS = null;

	private IndexSearcher searcher;
	private QueryParser contentQueryParser;
	private QueryParser nameQueryParser;
	private QueryParser pathQueryParser;
	private QueryParser imgPathQueryParser;
	private QueryParser altQueryParser;

	
	public DocSearch(Directory indexedDir) throws CorruptIndexException, IOException {	
		searcher = new IndexSearcher(indexedDir);
		
		contentQueryParser = new QueryParser(
				DocIndexer.FIELD_DOC_TEXT, DocIndexer.getAnalyzer());
		nameQueryParser = new QueryParser(
				DocIndexer.FIELD_DOC_NAME, DocIndexer.getAnalyzer());
		pathQueryParser = new QueryParser(
				DocIndexer.FIELD_DOC_PATH, DocIndexer.getAnalyzer());
		imgPathQueryParser = new QueryParser(
				DocIndexer.FIELD_DOC_IMGPATH, DocIndexer.getAnalyzer());
		altQueryParser = new QueryParser(
				DocIndexer.FIELD_DOC_ALT, DocIndexer.getAnalyzer());

	}
	public Hits searchContent(String querystr) {
		return runSearch(
				parseQuery(contentQueryParser, querystr));
	}
	
	public Hits searchName(String querystr) {
		return runSearch(
				parseQuery(nameQueryParser, querystr));
	}
	
	public Hits searchPath(String querystr) {
		return runSearch(
				parseQuery(pathQueryParser, querystr));
	}
	
	
	public Hits searchCombined(String querystr) {
		
		BooleanQuery combiner = new BooleanQuery();
		
		Query[] queries = new Query[5];
		queries[0] = parseQuery(contentQueryParser, querystr);
		queries[1] = parseQuery(nameQueryParser, querystr);
		queries[2] = parseQuery(pathQueryParser, querystr);
		queries[3] = parseQuery(imgPathQueryParser, querystr);
		queries[4] = parseQuery(altQueryParser, querystr);
		
		//return ResultCombiner.simpleCombine(allHits);
		return runSearch(combiner.combine(queries));
	}
	
	
	/*private Hits searchAlt(String querystr) {
		return runSearch(
				parseQuery(altQueryParser, querystr));
	}
	private Hits searchImgPath(String querystr) {
		return runSearch(
				parseQuery(imgPathQueryParser, querystr));	
	}*/
	public void close() {
		try {
			searcher.close();
		} catch (IOException e) {
			Logger.warn(this, "Unable to close searcher: " + e.getMessage());
		}
		
	}
	
	private Query parseQuery(QueryParser queryParser, String querystr) {
		Query query = null;
		try {
			query = queryParser.parse(querystr);
		} catch (ParseException e) {
			Logger.warn(this, "Error parsing query " 
					+ querystr + ": " + e.getMessage());
			return null;
		}
		return query;
	}
	
	
	private Hits runSearch(Query query) {
		Logger.info(this, "Searching for " + query.toString());
		Hits hits = null;
		try {
			hits = searcher.search(query);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return hits;
	}
	


}
